
//
//  LSMineVController.m
//  LookScore
//
//  Created by wr on 2017/12/6.
//  Copyright © 2017年 ChongQingXunTiCompany. All rights reserved.
//

#define labelWidth (self.width-70)/3.0f
#import "LSMineVController.h"
#import "LSMineListCell.h"
#import "LSMineView.h"
#import <UIButton+WebCache.h>
#import "LSLoginVController.h"
#import "LSNavigationViewController.h"
#import "LSMyMessageVController.h"//我的消息
#import "LSSettingVController.h"//设置
#import "LSUserFeedbackVController.h"//用户反馈
#import "LSPhoneVerificationVController.h"//手机验证
#import "LSAboutLookScoreVController.h"//关于看比分
#import "LSPersonalDataVController.h"//个人资料
#import "LSPersonalHomePageVController.h"//个人主页
#import "LSApplyExpertViewController.h"//申请专家
#import "LSEditIntroduceViewController.h"//编辑个性签名
#import "LSAccountCoder.h"
#import "LSAccountArchive.h"
#import "UIViewController+XYSideCategory.h"

//#import "UIViewController+CWLateralSlide.h"
#import "XYSideViewController.h"
@interface LSMineVController ()<UITableViewDelegate,UITableViewDataSource>

@property(strong,nonatomic)LSMineView *mineView;

@property(strong,nonatomic)UITableView *tableView;

@property(strong,nonatomic)NSArray *imgArray;

@end

@implementation LSMineVController

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
//    [notifiCenter addObserver:self selector:@selector(resetPwdAction) name:@"ISLOGIN" object:nil];
//    [notifiCenter addObserver:self selector:@selector(resetPwdAction) name:@"LOGINSUCCESS" object:nil];
//    [notifiCenter addObserver:self selector:@selector(getUserInfo) name:@"LOADCIRCLEDATA" object:nil];
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:YES];
    MyLog(@"已消失");
//    self.navigationController.navigationBar.translucent = YES;
}
-(UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]init];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor =LSBackGroundColor;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.scrollEnabled = NO;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _tableView;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    [notifiCenter addObserver:self selector:@selector(resetPwdAction) name:@"ISLOGIN" object:nil];
    [notifiCenter addObserver:self selector:@selector(resetPwdAction) name:@"LOGINSUCCESS" object:nil];
    [notifiCenter addObserver:self selector:@selector(getUserInfo) name:@"LOADCIRCLEDATA" object:nil];
    [self initUI];
}


- (void)viewDidAppear:(BOOL)animated{
    
    [super viewDidAppear:animated];
    
    CGRect rect = self.view.frame;
    
    [self.view.superview sendSubviewToBack:self.view];
    
    self.view.frame = rect;
    
}
- (void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
    
    
}
- (void)getUserInfo{
    [LSHttpEnclosureTool GET:LSUserInfo parameters:@{@"cid":[LSAccountArchive account].Cid} resBlock:^(id responseObject) {
        MyLog(@"userInfo:%@",responseObject);
        if (responseObject) {
            LSAccountCoder *coderModel1=[LSAccountCoder mj_objectWithKeyValues:responseObject];
            LSAccountCoder *coderModel = [LSAccountArchive account];
            coderModel.AttentionsCount = coderModel1.AttentionsCount;
            coderModel.FansCount = coderModel1.FansCount;
            coderModel.TopicPublishCount = coderModel1.TopicPublishCount;
            [LSAccountArchive saveAccount:coderModel];
            [self.mineView addControlData];
        }
    }];
}



- (void)initUI{
    self.navigationController.navigationBar.hidden=YES;
//    self.view.backgroundColor= [UIColor colorWithRed:0 green:0 blue:0 alpha:0.3];
    self.imgArray=@[@[@"个人主页"],@[@"我的消息",@"用户反馈",@"申请专家"],@[@"关于看比分",@"设置"]];
//    self.imgArray=@[@[@"个人主页"],@[@"我的消息",@"用户反馈"],@[@"关于看比分",@"设置"]];
    
//    UISwipeGestureRecognizer * recognizer = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipeFrom:)];
//    [recognizer setDirection:(UISwipeGestureRecognizerDirectionLeft)];
//    [self.view addGestureRecognizer:recognizer];
    
    self.tableView.tableHeaderView=[self headerView];
    [self.view addSubview:self.tableView];
    self.tableView.sd_layout.leftEqualToView(self.view).topEqualToView(self.view).offset(-LSStatusBarHeight).rightEqualToView(self.view).offset(-LSScreenWidth * 0.25).bottomEqualToView(self.view);
    
    
//    [notifiCenter addObserver:self selector:@selector(resetPwdAction) name:@"ISLOGIN" object:nil];
    
    
}

- (void)resetPwdAction{
    [self.mineView addControlData];
}
#pragma mark 🔥 tableViewDelegate tableViewDataSource

-(UIView *)headerView{
    LSMineView *mineView=[[LSMineView alloc]initWithFrame:CGRectMake(0, 0, LSScreenWidth-70, 220)];
    mineView.loginOrReplacePhotoBlock = ^(KLoginOrReplacePhotoType type) {
        [self loginOrReplacePhotoByType:type];
    };
    mineView.editWordsBlock = ^{
        [self EditIntroduceEvent];
    };
    [self.view addSubview:mineView];
    self.mineView=mineView;
    return mineView;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 3;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    NSArray *array=self.imgArray[section];
    
    return array.count;

}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return LSControlHeight;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    UIView *backView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, self.tableView.width,10)];
    return backView;
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section!=0) {
        return 10;
    }
    return 0;
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    LSMineListCell *cell=[LSMineListCell mineListTableViewCellByTableView:tableView];
    NSArray *array=self.imgArray[indexPath.section];
    cell.titleLab.text=array[indexPath.row];
    
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (![LSAccountArchive isLogin]) {
        
        if (indexPath.section==2) {
            if (indexPath.row==0){
                LSAboutLookScoreVController *aboutVC=[[LSAboutLookScoreVController alloc]init];
                [self presentRootViewController:aboutVC animated:YES completion:nil];
            }else{
                
                LSSettingVController *settingVC=[[LSSettingVController alloc]init];
                [self presentRootViewController:settingVC animated:YES completion:nil];
            }
        }else {
            LSLoginVController *loginVC=[[LSLoginVController alloc]init];
            [self presentRootViewController:loginVC animated:YES completion:nil];
            loginVC.loginSuccessBlock = ^{
                [self.mineView addControlData];
            };
        }
        
        //123
    }else{
        if (indexPath.section==0) {
            if (indexPath.row==0) {
               
                LSPersonalHomePageVController *homePageVC=[[LSPersonalHomePageVController alloc]init];
                homePageVC.userCid = [LSAccountArchive account].Cid;
                [self XYSidePushViewController:homePageVC animated:YES];
               
               
                homePageVC.PersonalDataChangedBlock = ^{
                    [self getUserInfo];
                };
            }
        }else if (indexPath.section==1){
            if (indexPath.row==0) {
                
                LSMyMessageVController *messageVC=[[LSMyMessageVController alloc]init];
                [self XYSidePushViewController:messageVC animated:YES];
//                [self cw_pushViewController:messageVC drewerHiddenDuration:0.01];
                
            }else if (indexPath.row==1){
                LSUserFeedbackVController *feedbackVC=[[LSUserFeedbackVController alloc]init];
                [self XYSidePushViewController:feedbackVC animated:YES];

//                [self presentRootViewController:feedbackVC animated:YES completion:nil];

            }
            else if (indexPath.row==2){
                LSApplyExpertViewController *applyExpertVC=[[LSApplyExpertViewController alloc]init];
                [self XYSidePushViewController:applyExpertVC animated:YES];
            }
        }else{
            if (indexPath.row==0){
                LSAboutLookScoreVController *aboutVC=[[LSAboutLookScoreVController alloc]init];
//                [self cw_pushViewController:aboutVC drewerHiddenDuration:0.01];
                [self XYSidePushViewController:aboutVC animated:YES];
            }else{
                
                LSSettingVController *settingVC=[[LSSettingVController alloc]init];
                settingVC.logOutSuccessBlock = ^{
                    [self.mineView addControlData];
                };
//                [self presentRootViewController:settingVC animated:YES completion:nil];
//                [self cw_pushViewController:settingVC drewerHiddenDuration:0.01];
                [self XYSidePushViewController:settingVC animated:YES];
            }
            
        }
    }
}

#pragma mark 🔥 事件
- (void)EditIntroduceEvent{
    LSEditIntroduceViewController *EIVC = [[LSEditIntroduceViewController alloc] init];
    EIVC.PersonalDataChangedBlock = ^{
        [self.mineView addControlData];
    };
    [self presentViewController:EIVC animated:YES completion:nil];
}

#pragma mark 🔥 Login Or ReplaceHeadImage

-(void)loginOrReplacePhotoByType:(KLoginOrReplacePhotoType )type{
    
    switch (type) {
        case KLoginOrReplacePhotoIsLogin :{
            
            LSLoginVController *loginVC=[[LSLoginVController alloc]init];
           [self presentRootViewController:loginVC animated:YES completion:nil];
//            [self presentViewController:loginVC animated:YES completion:nil];
            loginVC.loginSuccessBlock = ^{
                [self.mineView addControlData];
            };
            

        }
            break;
        case KLoginOrReplacePhotoIsReplace :{
            
            LSPersonalDataVController *personDataVC=[[LSPersonalDataVController alloc]init];
            [self presentRootViewController:personDataVC animated:YES completion:nil];
            personDataVC.PersonalDataChangedBlock = ^{
                [self.mineView addControlData];
            };
    
        }
            break;
        
        default:
            break;
    }
   
}

//#pragma mark 🔥 UISwipeGestureRecognizerDirectionLeft
//-(void)handleSwipeFrom:(UISwipeGestureRecognizer *)SwipeGesture{
//
//
//    [UIView animateWithDuration:1.f animations:^{
//        self.view.x = -LSScreenWidth;
//        self.view.y = LSScreenHeight;
//    } completion:^(BOOL finished) {
//        [self dismissViewControllerAnimated:YES completion:nil];
//
//    }];
//
//
//}


- (void)dealloc{
    
    [notifiCenter removeObserver:self];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
